package com.yyk.yykywd.mvvm.adapter

import android.text.Spannable
import android.text.SpannableStringBuilder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.yyk.yykywd.databinding.ItemAgentInfoBinding
import com.yyk.yykywd.databinding.ItemHospitalBinding
import com.yyk.yykywd.mvvm.entity.SearchCustomerBean
import com.yyk.yykywd.util.setTextForegroundColor


const val SEARCH_AGENT = 1
const val SEARCH_HOSPITAL = 2

class SearchCustomerAdapter
    : ListAdapter<SearchCustomerBean.Data, RecyclerView.ViewHolder>(SearchCustomerDiffCallback()) {
    private var mListener: ((Int, View) -> Unit)? = null
    var searchText: String = ""

    //lambda表达式作为函数中的参数
    fun setOnItemChildClickListener(mListener: (position: Int, view: View) -> Unit) {
        this.mListener = mListener
    }

    override fun getItemViewType(position: Int): Int {
        val data = getItem(position)
        if (data.client_type == 2) {
            return SEARCH_AGENT
        } else {
            return SEARCH_HOSPITAL
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val data = getItem(position)
        holder.itemView.setOnClickListener {
            mListener?.invoke(position, it)
        }
        if (holder is AgentViewHolder) {
            holder.binding.search = data
            holder.binding.adapter = this
            holder.binding.executePendingBindings()
        } else {
            (holder as HospitalViewHolder).binding.search = data
            holder.binding.adapter = this
            holder.binding.executePendingBindings()
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            SEARCH_AGENT -> {
                val binding = ItemAgentInfoBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                AgentViewHolder(binding)
            }
            else -> {
                val binding = ItemHospitalBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                HospitalViewHolder(binding)
            }
        }
    }

    class AgentViewHolder(val binding: ItemAgentInfoBinding)
        : RecyclerView.ViewHolder(binding.root)

    class HospitalViewHolder(val binding: ItemHospitalBinding)
        : RecyclerView.ViewHolder(binding.root)
}

@BindingAdapter(value = ["wholeText", "searchText"], requireAll = true)
fun TextView.setSpan(whole_text: String, searchText: String) {
    if (searchText.isNotEmpty()) {
        setTextForegroundColor(context, this, searchText, whole_text)
    } else {
        text = whole_text
    }
}

class SearchCustomerDiffCallback : DiffUtil.ItemCallback<SearchCustomerBean.Data>() {

    override fun areItemsTheSame(oldItem: SearchCustomerBean.Data, newItem: SearchCustomerBean.Data): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: SearchCustomerBean.Data, newItem: SearchCustomerBean.Data): Boolean {
        return oldItem == newItem
    }
}